package view.subwindosw;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import model.entity.Company;
import model.entity.Medicine;
import model.initializeEntity.IntCompany;
import model.initializeEntity.IntMedicine;
import table.GenericTableModel;
import view.main.Main;
import BaseClass.BaseSubFrame;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MedicineFrame extends BaseSubFrame<Medicine> {
	private JTextField textField;
	private JComboBox comboBox = new JComboBox();

	public MedicineFrame() throws ClassNotFoundException, SQLException {
		JTableModel = new GenericTableModel<Medicine>(new Medicine());
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				new Main().setMedicine_windows(false);
			}
		});

		setTitle("藥品管理");
		setBounds(100, 100, 753, 491);
		getContentPane().setLayout(null);

		JLabel label = new JLabel("新增藥品：");
		label.setFont(new Font("新細明體", Font.PLAIN, 20));
		label.setBounds(40, 44, 128, 39);
		getContentPane().add(label);

		textField = new JTextField();
		textField.setBounds(143, 49, 200, 29);
		getContentPane().add(textField);
		textField.setColumns(10);
		comboBox.setFont(new Font("新細明體", Font.PLAIN, 16));

		comboBox.setBounds(382, 49, 144, 29);
		getContentPane().add(comboBox);

		JButton button = new JButton("新增");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Medicine m = new Medicine();
				m.setOid((long) (comVs.size() + 1));
				m.setCompanyNmae(comboBox.getSelectedItem().toString());
				m.setMedicineName(textField.getText());
				m.setCreatdate(new Timestamp(0));
				m.setPrice(0);
				m.setUpdatedate(new Timestamp(0));
				comVs.addElement(m);
				JTableModel.fireTableDataChanged();
			}
		});
		button.setFont(new Font("新細明體", Font.PLAIN, 20));
		button.setBounds(583, 48, 111, 31);
		getContentPane().add(button);
		setLocationRelativeTo(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(40, 97, 654, 267);
		getContentPane().add(scrollPane);
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setFont(new Font("新細明體", Font.PLAIN, 20));
		scrollPane.setViewportView(table);
		table.getTableHeader().setReorderingAllowed(false);
		table.getAutoResizeMode();
		table.setRowHeight(25);
		Initialize();

	}

	public JTextField getTextField() {
		return textField;
	}

	public void setTextField(JTextField textField) {
		this.textField = textField;
	}

	@Override
	public void Initialize() throws ClassNotFoundException, SQLException {
		JTableModel.setDataVector(comVs);
		table.setModel(JTableModel);
		List<Company> comps;
		comps = null;
		for (Company c : comps) {
			comboBox.addItem(c.getComPanyName());
		}

	}

}
